<?php
namespace app\backman\controller;
use app\backman\model\Meal;
use \app\common\controller\AuthBack;
use app\common\model\WxAdmin;
use app\common\model\Scurl;
use lib\CashStatus;
use think\Db;
use think\facade\Env;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use Intervention\Image;
use GuzzleHttp;



class Detection extends AuthBack{


    public function initialize(){
        parent::initialize();
    }

    public function index(){
        $model = new WxAdmin;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : '20';

        $where = [];
        if( !empty(input('title')) ){
            $where[] = ['wx.weixin', 'like', "%".input('title')."%"];
        }
        
        if( !empty(input('urlid')) || input('urlid')==='0' ){
            $where[] = ['wx.urlid','eq',input('urlid')];
        }
        if( !empty(input('state')) || input('state')==='0' ){
            $where[] = ['wx.state','eq',input('state')];
        }

        $totalNum = $model::alias('wx')->join('scurl s', 's.id = wx.urlid')->where($where)->count();
        $Scurl = Scurl::select();
        if(request()->isAjax()){
            $page = isset($_GET['page']) ? $_GET['page'] : '1';
            $limit = isset($_GET['limit']) ? $_GET['limit'] : '20';
            $startNum = ($page - 1) * $limit;
            $totalPage = ceil($totalNum/$limit);

            $list = $model->alias('wx')->join('scurl s', 's.id = wx.urlid')->where($where)->limit($startNum.','.$limit)->field('wx.*,s.url')->select();
            
            if(empty($list)){
                return json(['code'=>0,'data'=>[],'total'=>$totalPage,'count'=>$totalNum]);
            }elseif($page > $totalPage){
                return json(['code'=>0,'data'=>[],'total'=>$totalPage+1,'count'=>$totalNum]);
            }else{
                $nList = array();
                if(!empty($list)){
                    foreach($list as $k=>$vo){
                        $nList[$k] = $vo;
                        $nList[$k]['op'] = url('option',['id'=>$vo['id']]);
                        $nList[$k]['cp'] = url('status', ['id'=>$vo['id'] ] );
                    }
                }
                $return = [
                    'code'=>0,
                    'msg'=>'',
                    'count'=>$totalNum,
                    'data'=>$nList
                ];
                return json($return);
            }
        }else{
            $this->assign('totalNum',$totalNum);
            $this->assign('Scurl',$Scurl);
            return view();
        }
    }


   

    /**
     * 检测流程删除
     */
    public function del(){
    
        $res = DB::name('wxAdmin')->where('id',$this->data['id'])->delete();

        if( $res ){
            return $this->success('操作成功',url('index'));
        }else{
            return $this->error('删除失败');
        }

    }


    public function status(){
        $status = DB::name('wxAdmin')->where('id',$this->data['id'])->value('state');
        $status = $status == 1 ? 0 : 1;
        $res = DB::name('wxAdmin')->where('id',$this->data['id'])->update(['state'=>$status]);
        if( $res ){
            return $this->success('操作成功',url('index'));
        }else{
            return $this->error('操作失败');
        }

    }

    /**
     * 编辑检测流程信息
     * @param int $id
     * @return \think\response\View|void
     */
    public function option($id = 0){
        if(!request()->isPost()){
            $Scurl = Scurl::select();
            $info = WxAdmin::get($id);
            $this->assign('info',$info);
            $this->assign('Scurl',$Scurl);
            return view();
        }else{
            if( empty( $this->data['weixin'] ) ) return $this->error( '微信号不能为空' );
            if( empty( $this->data['urlid'] ) ) return $this->error('绑定域名不能为空') ;
            $obj = new WxAdmin();
            if($this->data['id']){
                // 编辑
                $state = $obj->saveData($this->data,'edit');
            }else{
                // 新增
                $state = $obj->saveData($this->data);
            }
            if($state){
                if($this->data['id']){
                    return $this->success('操作成功',url('index'));
                }else{
                    return $this->success('操作成功',url('index'));
                }
                
            }else{
                return $this->error($obj->getError());
            }
        }
    }

   


    



}